package display;

import it.unibo.is.interfaces.IBasicEnvAwt;

/**
 * @author Stefano&Davide
 * @version 1.0
 */

public abstract class DisplayOdometer extends Display {

	public DisplayOdometer(IBasicEnvAwt env) {
		super(env);
	}

	@ Override
	public void update(String val){
		boolean check=checkVal(val);
		if(check){
			super.update(val);
		}else if(checkDigitalVal(val)){
			super.update(toDisplayStr(val));
		}else{
			super.update("");
		}
	}
	
	private String toDisplayStr(String val) {
		String vs=val;
		for(int i=0;i<6-val.length();i++){
			vs="0"+vs;
		}
		return vs;
	}
	
	private boolean checkDigitalVal(String val) {
		for(int i=0;i<val.length();i++){
			if(! isDigit(val.charAt(i))) return false;
		}
		return true;
	}
	
	private boolean checkVal(String val) {
		if(val.length()!=6)return false;
		for(int i=0;i<val.length();i++){
			if(! isDigit(val.charAt(i))) return false;
		}
		return true;
	}
	
	private boolean isDigit(char ch) {
		return ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4'
				|| ch=='5' || ch=='6' ||ch=='7' || ch=='8' || ch=='9';
	}
	
}
